package com.daona.application.controller;

import cn.hutool.core.util.IdUtil;
import com.daona.infrastructure.config.MinioConfig;
import com.daona.infrastructure.minio.MinioUtils;
import com.daona.model.view.OssView;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/oss")
public class OssController {

    private final MinioUtils minioUtils;
    private final MinioConfig minioConfig;

    /**
     * 文件上传
     *
     * @param files
     */
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile[] files) {
        try {
            for (MultipartFile file : files) {
                //文件名
                String fileName = file.getOriginalFilename();
                // 生成文件上传唯一名称
                String suffix = fileName.split("\\.")[1];
                String objectKey = IdUtil.fastSimpleUUID().concat(".").concat(suffix);
                String contentType = file.getContentType();
                minioUtils.uploadFile(minioConfig.getBucketName(), file.getInputStream(), objectKey, contentType);
            }
        } catch(Exception e){
            log.error("上传失败");
            return OssView.builder().data("上传失败").build();
        }
        return OssView.builder().data("上传成功").build();
    }

    /**
     * 删除
     *
     * @param fileName
     */
    @DeleteMapping("/")
    public void delete(@RequestParam("fileName") String fileName) {
        minioUtils.removeFile(minioConfig.getBucketName(), fileName);
    }

    /**
     * 获取文件信息
     *
     * @param fileName
     * @return
     */
    @GetMapping("/info")
    public String getFileStatusInfo(@RequestParam("fileName") String fileName) {
        return minioUtils.getFileStatusInfo(minioConfig.getBucketName(), fileName);
    }

    /**
     * 获取文件外链
     *
     * @param fileName
     * @return
     */
    @GetMapping("/url")
    public String getPresignedObjectUrl(@RequestParam("fileName") String fileName) {
        return minioUtils.getPresignedObjectUrl(minioConfig.getBucketName(), fileName);
    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     */
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try {
            InputStream fileInputStream = minioUtils.getObject(minioConfig.getBucketName(), fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName); //test.docx
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            fileInputStream.transferTo(response.getOutputStream());
            //IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败");
        }
    }
}
